<?php
/**
 * Exstension for BuddyPress
 *
 * This file extends WPSocialStreamer to support actions from BuddyPress
 *
 * @package WPSocialStreamer
 * @subpackage ExstensionBuddyPress
 */
 
// No direct access!
if ( !defined('ABSPATH') ) exit;

// Register needed hooks
add_action( 'bp_activity_posted_update', 'wpsocialstreamer_buddypress_update', 10, 3);
add_action( 'xprofile_avatar_uploaded', 'wpsocialstreamer_buddypress_avatar');
add_action( 'friends_friendship_accepted', 'wpsocialstreamer_buddypress_friend', 10, 3);
add_action( 'groups_group_create_complete', 'wpsocialstreamer_buddypress_group_add');
add_action( 'groups_join_group', 'wpsocialstreamer_buddypress_group_join', 10, 2);
// Add extension options filter 
add_filter( 'wpsocialstreamer_extensions', 'wpsocialstreamer_extension_buddypress_settings' );

/**
 * Extend the extension array
 * 
 * @param type $extensions_array
 * @return type
 */
function wpsocialstreamer_extension_buddypress_settings( $extensions_array ) {
  
  $extensions_array['buddypress'] = array (
    /* Set the title that will be displayed on the settings page */
    'title'     => __('BuddyPress', 'wpsocialstreamer'),
    /* Set a function that should be used to checked if the plugin is installed */
    'function'	=> 'bp_version',
    /* Set the link to the plugin */
    'link'		=> 'http://buddypress.org/',
    /* Extension status */
    'active' => false,
    /* Define an array with available events / actions */
    /* ATTENTION: events should be named exactly like the corresponding action! */      
    'events' => array (
      'bp_activity_posted_update'  => array (
        /* Set the default message template */
        'template'  => __("I've updated my activity stream: %CONTENT%", 'wpsocialstreamer'),
        /* Set the event description for user settings page. */
        /* Share this event when: */
        'title'     => __("I post an activity update.", 'wpsocialstreamer'),
        /* Define some placeholders that admins can use to generate message text */
        /* Set to false if no placeholders are available: 'placeholders' => false */
        'placeholders' => array ( 
          /* Set placeholder name and a short description */
          'CONTENT'   => __("Activity content", 'wpsocialstreamer')
        ) /* END placeholders */
      ), /* END event */
      'xprofile_avatar_uploaded'  => array (
        /* Set the default message template */
        'template'  => __("I just added a new avatar on %SITE_NAME%!", 'wpsocialstreamer'),
        /* Set the event description for user settings page. */
        /* Share this event when: */
        'title'     => __("I add an avatar.", 'wpsocialstreamer'),
        /* Define some placeholders that admins can use to generate message text */
        /* Set to false if no placeholders are available: 'placeholders' => false */
        'placeholders' => array ( 
          /* Set placeholder name and a short description */
          'SITE_NAME'   => __("Site name", 'wpsocialstreamer')
        ) /* END placeholders */
      ), /* END event */
      'friends_friendship_accepted'  => array (
        /* Set the default message template */
        'template'  => __("Wohoo, I just made a new friend on %SITE_NAME%!", 'wpsocialstreamer'),
        /* Set the event description for user settings page. */
        /* Share this event when: */
        'title'     => __("I add a new friend.", 'wpsocialstreamer'),
        /* Define some placeholders that admins can use to generate message text */
        /* Set to false if no placeholders are available: 'placeholders' => false */
        'placeholders' => array ( 
          /* Set placeholder name and a short description */
          'SITE_NAME'   => __("Site name", 'wpsocialstreamer')
        ) /* END placeholders */
      ), /* END event */
      'groups_group_create_complete'  => array (
        /* Set the default message template */
        'template'  => __("I just made the group %GROUP_NAME% on %SITE_NAME%!", 'wpsocialstreamer'),
        /* Set the event description for user settings page. */
        /* Share this event when: */
        'title'     => __("I create a new group.", 'wpsocialstreamer'),
        /* Define some placeholders that admins can use to generate message text */
        /* Set to false if no placeholders are available: 'placeholders' => false */
        'placeholders' => array ( 
          /* Set placeholder name and a short description */
          'SITE_NAME'   => __("Site name", 'wpsocialstreamer'),
          'GROUP_NAME'  => __("Group name", 'wpsocialstreamer')
        ) /* END placeholders */
      ), /* END event */
      'groups_join_group'  => array (
        /* Set the default message template */
        'template'  => __("I just joined the group %GROUP_NAME% on %SITE_NAME%!", 'wpsocialstreamer'),
        /* Set the event description for user settings page. */
        /* Share this event when: */
        'title'     => __("I join a new group.", 'wpsocialstreamer'),
        /* Define some placeholders that admins can use to generate message text */
        /* Set to false if no placeholders are available: 'placeholders' => false */
        'placeholders' => array ( 
          /* Set placeholder name and a short description */
          'SITE_NAME'   => __("Site name", 'wpsocialstreamer'),
          'GROUP_NAME'  => __("Group name", 'wpsocialstreamer')
        ) /* END placeholders */
      ) /* END event */
    )
  );
    
  return $extensions_array;
}

/**
 * Called when user upadtes his activity stream
 * 
 * @global obj $wpss
 * @param string $content
 * @param int $user_id
 * @param int $activity_id
 * @return type
 */
function wpsocialstreamer_buddypress_update($content, $user_id, $activity_id) {
  global $wpss;
   
  if ( empty($content) ) return;
 
  // Check if this event is active
  if ( $wpss->is_active('buddypress', 'bp_activity_posted_update') ) {
    
    $wpss->set_raw_message( 'buddypress', 'bp_activity_posted_update' );
    
    // Set needed data
    $wpss->set_data( array( 
      'CONTENT' => $content,
        /* Every extension must contain the LINK field. Values: link or false */
        'LINK'   => bp_activity_get_permalink( $activity_id )  
      ) 
    );
    
    // Generate and share the message
    $wpss->generate_and_share();
  }
   
  return;
}

/**
 * Called when user updates his avatar
 * 
 * @global obj $wpss
 * @return type
 */
function wpsocialstreamer_buddypress_avatar() {
  global $wpss;
    
  // Check if this event is active
  if ( $wpss->is_active('buddypress', 'xprofile_avatar_uploaded') ) {
    
    $wpss->set_raw_message( 'buddypress', 'xprofile_avatar_uploaded' );
    
    // Set needed data
    $wpss->set_data( array( 
        'SITE_NAME' => get_bloginfo('name'),
        /* Every extension must contain the LINK field. Values: link or false */
        'LINK'      => get_site_url()
      ) 
    );
    
    // Generate and share the message
    $wpss->generate_and_share();
  }
   
  return;
}

/**
 * Called when user accepts a friend
 * 
 * @global obj $wpss
 * @param int $friendship_id
 * @param int $inviter_id
 * @param int $invitee_id
 * @return type
 */
function wpsocialstreamer_buddypress_friend($friendship_id, $inviter_id, $invitee_id) {
  global $wpss;
  
  if ( empty($friendship_id) ) return;
    
  // Check if this event is active
  if ( $wpss->is_active('buddypress', 'friends_friendship_accepted' ) ) {
    
    $wpss->set_raw_message( 'buddypress', 'friends_friendship_accepted' );
    
    // Set needed data
    $wpss->set_data( array( 
        'SITE_NAME' => get_bloginfo('name'),
        /* Every extension must contain the LINK field. Values: link or false */
        'LINK'      => get_site_url()
      )
    );
    
    // Generate and share the message
    $wpss->generate_and_share();
  }
   
  return;
}

/**
 * Called when user creates a new group
 * 
 * @global obj $wpss
 * @param int $group_id
 * @return type
 */
function wpsocialstreamer_buddypress_group_add( $group_id ) {
  global $wpss;
  
  if ( empty($group_id) ) return;
  
  $group = groups_get_group( array( 'group_id' => $group_id ) );
  
  // Check if this event is active
  if ( $wpss->is_active('buddypress', 'groups_group_create_complete' ) ) {
    
    $wpss->set_raw_message( 'buddypress', 'groups_group_create_complete' );
    
    // Set needed data
    $wpss->set_data( array( 
        'GROUP_NAME' => $group->name,
        'SITE_NAME'  => get_bloginfo('name'),
        /* Every extension must contain the LINK field. Values: link or false */
        'LINK'      => bp_get_group_permalink( $group_id )
        ) 
    );
    
    // Generate and share the message
    $wpss->generate_and_share();
  }
   
  return;
}

/**
 * Called when user joins a group
 * 
 * @global obj $wpss
 * @param int $group_id
 * @param int $user_id
 * @return type
 */
function wpsocialstreamer_buddypress_group_join($group_id, $user_id) {
  global $wpss;
  
  if ( empty($group_id) ) return;
  
  $group = groups_get_group( array( 'group_id' => $group_id ) );
  
  // Check if this event is active
  if ( $wpss->is_active('buddypress', 'groups_join_group') ) {
    
    $wpss->set_raw_message( 'buddypress', 'groups_join_group' );
    
    // Set needed data
    $wpss->set_data( array( 
        'GROUP_NAME' => $group->name,
        'SITE_NAME'  => get_bloginfo('name'),
        /* Every extension must contain the LINK field. Values: link or false */
        'LINK'      => bp_get_group_permalink( $group_id )      
        ) 
    );
    
    // Generate and share the message
    $wpss->generate_and_share();
  }
   
  return;
}
?>